﻿#ifndef SINGLETON_H
#define SINGLETON_H
#include <QObject>
#include <QMutex>
#include <QScopedPointer>

template <typename T>
class Singleton
{
public:
    static T &getInstance(QObject *parent = nullptr)
    {
        static QMutex mutex;
        QMutexLocker locker(&mutex);

        if (!_instance)
        {
            _instance.reset(new T(parent));
        }
        return *_instance;
    }
    Singleton(const Singleton &) = delete;
    Singleton &operator=(const Singleton &) = delete;

protected:
    Singleton() {}
    virtual ~Singleton() {}
    bool _initialized{false};

private:
    static QScopedPointer<T> _instance;
};

template <typename T>
QScopedPointer<T> Singleton<T>::_instance;
#endif
